package com.chang.bootplus.config.swagger;


import com.chang.bootplus.base.annotation.PublicVisit;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.util.UriComponentsBuilder;
import springfox.documentation.PathProvider;
import springfox.documentation.builders.*;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.DefaultPathProvider;
import springfox.documentation.spring.web.paths.Paths;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lcc
 * @create 2021-05-30
 * @注意 本内容仅限于dev414内部传阅，禁止外泄以及用于其他的商业目的
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config {

    @Value("${server.servlet.context-path}")
    private String servletContextPath;



    @Bean(value = "privateApi")
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .pathProvider(pathProvider())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chang.bootplus.controller"))
                .paths(PathSelectors.any())
                .build().groupName("所有接口")
                .globalRequestParameters(globalOperation());
    }


    @Bean(value = "publicApi")
    public Docket publicApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(PublicVisit.class))
                .paths(PathSelectors.any())
                .build().groupName("无需token验证接口").ignoredParameterTypes(HttpServletResponse.class, HttpServletRequest.class);
    }
    private List<RequestParameter> globalOperation(){
        //添加head参数配置start
        RequestParameterBuilder requestParameterBuilder = new RequestParameterBuilder();

        List<RequestParameter> pars = new ArrayList<>();
        /*配置请求头*/
        requestParameterBuilder.name("authorization").description("token令牌")                           .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("1")
                .allowEmptyValue(true))
                .in(ParameterType.HEADER);
        pars.add(requestParameterBuilder.build());
        return pars;
    }

    private PathProvider pathProvider() {
        return new DefaultPathProvider() {
            @Override
            public String getOperationPath(String operationPath) {
                operationPath = operationPath.replaceFirst(servletContextPath, "/");
                UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromPath("/");
                return Paths.removeAdjacentForwardSlashes(uriComponentsBuilder.path(operationPath).build().toString());
            }

            @Override
            public String getResourceListingPath(String groupName, String apiDeclaration) {
                apiDeclaration = super.getResourceListingPath(groupName, apiDeclaration);
                return apiDeclaration;
            }
        };
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("脚手架模版 API 文档")
                .description("")
                .version("1.0.0")
                .build();
    }
}
